package com.buba.service.impl;

import com.buba.mapper.FollowTopicMapper;
import com.buba.service.FollowTopicService;
import entity.FollowTopic;
import entity.FollowTopicEntity;
import entity.KudosEntity;
import entity.VO.FollowTopicVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 孔德嘉
 * @Description: TODO
 * @DateTime: 2023/3/6 19:00
 **/
@Service
public class FollowTopicServiceImpl implements FollowTopicService {
    //注入
    @Autowired
    private FollowTopicMapper followTopicMapper;
    /*
     * @param id
     * @return result.CommonResult
     * @author kdj
     * @describe:查询关注的话题
     * @date 2023/3/6 17:02
     */
    @Override
    public List<FollowTopicVO> getFollowTopicList(int id) {
        return followTopicMapper.getFollowTopicList(id);
    }

    /*
     * @param topicId 话题id
     * @param userId 用户id
     * @return int
     * @author kdj
     * @describe:修改话题被关注状态
     * @date 2023/3/9 11:52
     */
    @Override
    public int updateFollowTopicByUserId(int topicId, int userId) {
        int i =0;//定义i判断是否修改状态成功
        //查询话题有没有被该用户关注
        FollowTopicEntity followTopic = followTopicMapper.getFollowByUserId(topicId,userId);
        //判断数据库中是否有  关注过的记录
        if(followTopic!=null ){
            //如果数据库中有数据 被关注 del_flag改为1
            if(followTopic.getDelFlag()==0){
                followTopic.setDelFlag(1);
                i = followTopicMapper.upFollowStatus(followTopic);
            }else{
                //如果数据库中有数据   del_flag改为0
                followTopic.setDelFlag(0);
                i = followTopicMapper.upFollowStatus(followTopic);
            }
        }else{
            followTopic=new FollowTopicEntity();
            followTopic.setFollowTopicCoverTopicId(topicId);
            followTopic.setFollowTopicUserId(userId);
           //如果数据库中没数据  添加数据
            i = followTopicMapper.addFollowTopic(followTopic);
        }
        return i;
    }
}
